package com.etoak.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.etoak.common.constant.CommonConstant;
import com.etoak.entity.Order;
import com.etoak.mapper.OrderMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class OrderService {

    @Autowired
    OrderMapper orderMapper;

    @RabbitListener(queues = "order")
    public void cancel(String orderNo){
        Order order = getOrder(orderNo);
        //15分钟后，根据订单编号查询该订单，判断订单状态是否为新建状态，若是 改为取消状态
        if (ObjectUtils.isNotEmpty(order) && order.getOrderState().equals(CommonConstant.ORDER_CREATE_STATE)){
            order.setOrderState(CommonConstant.ORDER_CANCEL_STATE);
            order.setCreateTime(new Date());
            order.setCancelReason("超时未支付，自动取消！");
            orderMapper.updateById(order);
        }
    }

    private Order getOrder(String orderNo){
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",orderNo);
        Order order = orderMapper.selectOne(queryWrapper);
        return order;
    }
}
